package com.xd.study.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.SimpleAdapter;

import com.xd.study.R;
import com.xd.study.widget.DownRefreshListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by DGF on 2015/5/11.
 * 下拉刷新的ListView
 */
public class ListViewDownRefreshActivity extends Activity {

    private List<Map<String, Object>> data;
    private SimpleAdapter adapter;
    private DownRefreshListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_down_refresh);

        data = new ArrayList<Map<String,Object>>();
        for (int i=0;i<10;i++){
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("pic", R.drawable.toggle_button_off);
            map.put("txt", "慕课网"+i);
            map.put("info", "详细信息详细信息详细信息");
            data.add(map);
        }
        adapter = new SimpleAdapter(this, data,
                R.layout.listview_item,
                new String[]{"pic","txt", "info"},
                new int[]{R.id.item_image, R.id.item_text, R.id.item_info});

        listView = (DownRefreshListView)findViewById(R.id.down_refresh_listview);
        listView.setAdapter(adapter);

        /**
         * 实现刷新数据的接口
         */
        listView.setRefreshListener(new DownRefreshListView.IRefreshListener() {
            @Override
            public void onRefresh() {
                /**
                 * 添加延时，以便看清刷新效果
                 */
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        /**
                         * 获取最新数据
                         */
                        for (int i = 0; i < 2; i++) {
                            Map<String, Object> map = new HashMap<String, Object>();
                            map.put("pic", R.drawable.toggle_button_off);
                            map.put("txt", "慕课网" + i);
                            map.put("info", "刷新数据刷新数据刷新数据");
                            data.add(0, map);
                        }
                        adapter = new SimpleAdapter(ListViewDownRefreshActivity.this, data,
                                R.layout.listview_item,
                                new String[]{"pic", "txt", "info"},
                                new int[]{R.id.item_image, R.id.item_text, R.id.item_info});

                        /**
                         * 通知界面显示
                         */
                        adapter.notifyDataSetChanged();

                        /**
                         * 通知ListView刷新数据完毕
                         */
                        listView.refreshCompleted();
                    }
                }, 2000);
            }
        });
    }
}
